package com.idbk.chargestation.api;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.StringRequest;
import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.net.EHttpParams;
import com.idbk.chargestation.net.EHttpResponse;

import java.util.Map;
import java.util.Map.Entry;

/**
 * 一个简单的http封装类
 * @author lupc
 *
 */
public class EHttpWrapper {

	public final static String TAG = EHttpWrapper.class.getSimpleName();
	
	public static RequestQueue getRequestQueue(){
		return AppContext.getInstance().getVolleyQueue();
	}

	public static StringRequest get(String URL,final EHttpResponse response){
		Log.d(TAG, URL);
		StringRequest request = new StringRequest(
				com.android.volley.Request.Method.GET, 
				URL, 
				response.getmListener(), 
				response.getmErrorListener()){

					@Override
					protected void onFinish() {
						// TODO Auto-generated method stub
						super.onFinish();
						
						response.onEFinish();
					}
			
		};
		
		RetryPolicy policy = new DefaultRetryPolicy(
				300000, 
				0, 
				0f);
		request.setRetryPolicy(policy);
		
		getRequestQueue().add(request);
		return request;
	}
	
	public static StringRequest get(String URL,final Map<String, String> params,final EHttpResponse response){
		Log.d(TAG, URL);
		//get参数需要自己绑定，坑爹啊		
		StringBuilder sb = new StringBuilder(URL);
		int i = 1;
		for (Entry<String, String> entry : params.entrySet()) {
			if(i == 1){
				sb.append("?").append(entry.getKey()).append("=").append(entry.getValue());
			} else {
				sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
			}
			i++;
		}
		String urlWithParams = sb.toString();
		//
		StringRequest request = new StringRequest(
				com.android.volley.Request.Method.GET, 
				urlWithParams, 
				response.getmListener(), 
				response.getmErrorListener()){

					@Override
					protected void onFinish() {
						// TODO Auto-generated method stub
						super.onFinish();
						
						response.onEFinish();
					}			
										
		};
//		
		RetryPolicy policy = new DefaultRetryPolicy(
				300000, 
				0, 
				0f);
		request.setRetryPolicy(policy);
		
		getRequestQueue().add(request);
		return request;
	}

	/**
	 * 只有参数名称不重复的时候 才能调用（map会覆盖）
	 * @param URL url
	 * @param params 参数
	 * @param response 回调
	 */
	public static StringRequest post(String URL,final Map<String, String> params,final EHttpResponse response){
		Log.d(TAG, URL);
		StringRequest request = new StringRequest(
				com.android.volley.Request.Method.POST, 
				URL, 
				response.getmListener(), 
				response.getmErrorListener()){

					@Override
					protected void onFinish() {
						// TODO Auto-generated method stub
						super.onFinish();
						
						response.onEFinish();
					}

					@Override
					protected Map<String, String> getParams()
							throws AuthFailureError {
						return params;
					}				
										
		};
		
		RetryPolicy policy = new DefaultRetryPolicy(
				300000, 
				0, 
				0f);
		request.setRetryPolicy(policy);
		
		getRequestQueue().add(request);
		return request;
	}

	public static StringRequest post(String URL,final EHttpResponse response){
		Log.d(TAG, URL);
		StringRequest request = new StringRequest(
				com.android.volley.Request.Method.POST, 
				URL,
				response.getmListener(), 
				response.getmErrorListener()){

					@Override
					protected void onFinish() {
						// TODO Auto-generated method stub
						super.onFinish();
						response.onEFinish();
					}
			
		};
//		
		RetryPolicy policy = new DefaultRetryPolicy(
				300000, 
				0, 
				0f);
		request.setRetryPolicy(policy);
		
		getRequestQueue().add(request);
		return request;
	}

	public static StringRequest post(String URL, final EHttpParams params, final EHttpResponse response){
		Log.d(TAG, URL);
		StringRequest request = new StringRequest(
				com.android.volley.Request.Method.POST, 
				URL, 
				response.getmListener(), 
				response.getmErrorListener()){

					@Override
					protected void onFinish() {
						// TODO Auto-generated method stub
						super.onFinish();
						
						response.onEFinish();
					}

					@Override
					public byte[] getBody() throws AuthFailureError {
						//这里使用UTF-8
						return params.encodeParameters("utf-8");
					}
					
					@Override
					public String getBodyContentType() {
						//这里使用默认的UTF-8就可以了
						return super.getBodyContentType();
					}
								
		};
		
		RetryPolicy policy = new DefaultRetryPolicy(
				300000, 
				0, 
				0f);
		request.setRetryPolicy(policy);
		
		getRequestQueue().add(request);
		return request;
	}


}
